#include <stdio.h>
#define MAX 100
#define MAXCOST 0x7fffffff

int graph[MAX][MAX];

void prim(int graph[][MAX],int n){

	int mst[MAX];
	int sum = 0;
	for(int i = 1 ; i <= n; ++i){
		mst[i] = 0;
	}
	mst[1] = 1;
	
	for (int k = 0; k < n; ++k)
	{
		printf("寻找第一个点k=%d\n",k);
		int min = MAXCOST;
		int addNode = 0;
		int nowNode = 0;
		for(int i = 1; i <= n ; ++i){
			if(0!=mst[i]){
				printf("%d已经加入\n",i);
				for (int j = 1; j <= n; ++j)
				{
					printf("mst[j]=%d,j=%d\n",mst[j],j);
					if(min > graph[i][j] && mst[j]==0){
						min = graph[i][j];
						nowNode = i;
						addNode = j;
						printf("找到最小值%d,i = %d,j = %d\n",min,i,j);
					}
				}
			}

		}
		if(addNode!=0){
			mst[addNode] = nowNode;
			sum += min;
		}
		// printf("%d-%d-%d\n",mst[addNode],addNode,min);
	}
	printf("sum=%d\n",sum);
}
void display(int graph[][MAX],int n){
	printf("您输入的矩阵是：\n");
	for (int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n ; ++j){
			printf("%d ",graph[i][j]);
		}
		printf("\n");
	}
}
int main(){
	int i, j , k , m , n;
	int x, y, cost;
	scanf("%d%d",&m,&n);
	for(i = 1; i <= m; i ++){
		for(j = 1; j <= m; j++){
			graph[i][j] = MAXCOST;
		}
	}
	for(k = 1; k <= n; k++){
		scanf("%d%d%d",&i,&j,&cost);
		graph[i][j] = cost;
		graph[j][i] = cost;
	}
	display(graph,m);
	prim(graph,m);	
	return 0;

}